import wsgiref.handlers
from google.appengine.ext import webapp

import EventRecord

class Admin( webapp.RequestHandler ):

  def adminEvent( self, record ):

    filling =  {
        'title' : record.title
      , 'date' : record.date
      , 'time' : record.time
      , 'venue' : record.venue
      , 'id' : record.id
      , 'passcode' : record.passcode
    }

    self.response.out.write(
'''
<h3>Event Administration</h3>
<form action="update_event" method="post" accept-charset="utf-8">
    <table>
    <tr><td>Event Title:</td><td><input type="text" name="title" id="title" value="%(title)s" /></td></tr>
    <tr><td>Event Date:</td><td><input type="text" name="date" id="date" value="%(date)s" />(yyyy/mm/dd)</td></tr>
    <tr><td>Event Time:</td><td><input type="text" name="time" id="time" value="%(time)s" />(hh:mm - hh:mm)</td></tr>
    <tr><td>Event Venue:</td><td><input type="text" name="venue" id="venue" value="%(venue)s" /></td></tr>
    <tr><td colspan="2"><button type="submit">Update!</button></td></tr>
    </table>
    <input type="hidden" id="id" name="id" value="%(id)s"/>
    <input type="hidden" id="passcode" name="passcode" value="%(passcode)s"/>
</form>

<a href="event?id=%(id)s">Event Registration Page</a><br />
<a href="export?id=%(id)s&passcode=%(passcode)s">Export Guest List</a><br /><br />

<form action="broadcast" method="post" accept-charset="utf-8">
  <button type="submit">Broadcast Message to All Guests</button><br />
  <textarea cols="80" rows="24" id="msg" name="msg"></textarea>
  <input type="hidden" id="id" name="id" value="%(id)s"/>
  <input type="hidden" id="passcode" name="passcode" value="%(passcode)s"/>
</form>
''' % filling )
  # end of Admin.adminEvent

################################################################################

  def get( self ):
    self.response.headers['Content-Type'] = 'text/html'

    id = self.request.get( 'id' )
    passcode = self.request.get( 'passcode' )
    record = EventRecord.EventRecord \
      .gql( 'WHERE id = :1 AND passcode = :2', id, passcode ) \
      .get()

    if record:
      self.adminEvent( record )
      return

    self.response.out.write(
'''
<h3>Event Registration Admin</h3>

<form action="create_event" method="post" accept-charset="utf-8">
  <fieldset>
    <legend>Create Event</legend>
    <table>
    <tr><td>Event Title:</td><td><input type="text" name="title" id="title" /></td></tr>
    <tr><td>Event Date:</td><td><input type="text" name="date" id="date" />(yyyy/mm/dd)</td></tr>
    <tr><td>Event Time:</td><td><input type="text" name="time" id="time" />(hh:mm - hh:mm)</td></tr>
    <tr><td>Event Venue:</td><td><input type="text" name="venue" id="venue" /></td></tr>
    <tr><td>Admin Email:</td><td><input type="text" name="email" id="email" /></td></tr>
    <tr><td colspan="2"><button type="submit">Create!</button></td></tr>
    </table>
  </fieldset>
</form>

<a href="http://appengine.google.com/" target="_blank"><img
src="http://code.google.com/appengine/images/appengine-silver-120x30.gif"
alt="Powered by Google App Engine" style="border:none;" /></a>
'''
    )
  # end of Admin.get

# end of class Admin
